package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Donors;
import company.domain.Educationalarticles;
import company.domain.Rescuestations;
import company.service.RescuestationsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class RescuestationsController {
    @Resource
    RescuestationsService rescuestationsService;

    /*
     * 分页查询
     * */
    @RequestMapping("/selectAllRescuestations/{page}")
    public R selectAllRescuestations(@PathVariable("page") int page,@RequestBody Rescuestations rescuestations){
        QueryWrapper<Rescuestations> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(rescuestations.getName()!="" && !("".equals(rescuestations.getName())),"name",rescuestations.getName());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Rescuestations> list=this.rescuestationsService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 新增救助站
     * */
    @PostMapping("/AddRescuestations")
    public R AddRescuestations(@RequestBody Rescuestations rescuestations){
        this.rescuestationsService.save(rescuestations);
        return new R();
    }

    /*
     * 救助站删除
     * */
    @RequestMapping("/DelRescuestations/{id}")
    public R DelRescuestations(@PathVariable("id") int id){
        boolean remove = this.rescuestationsService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据救助站id查询
     * */
    @PostMapping("/selectRescuestations")
    public R selectRescuestations(@RequestBody Rescuestations rescuestations){
        Rescuestations byId = this.rescuestationsService.getById(rescuestations.getStationid());
        return new R().add("byId",byId);
    }

    /*
     * 修改救助站
     * */
    @PostMapping("/uploadRescuestations")
    public R uploadRescuestations(@RequestBody Rescuestations rescuestations){
        this.rescuestationsService.updateById(rescuestations);
        return new R();
    }

    @RequestMapping("/selectAllRescuestations2/{page}")
    public R selectAllRescuestations2(@PathVariable("page") int page){
        PageHelper.startPage(page,2);
        List<Rescuestations> list=this.rescuestationsService.list();
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

}
